USE PeDALS_DEV
GO
		DECLARE @varProvenanceID as Int;
		DECLARE @varSeriesID as Int;
		DECLARE @varGeographicLocation0 as VarChar(250);
		DECLARE @varGeographicLocation1 as VarChar(250);
		DECLARE @varSeriesSubject0 as VarChar(100);
		DECLARE @varSeriesSubject1 as VarChar(100);
		DECLARE @varSeriesKeyword0 as VarChar(50);
		DECLARE @varSeriesKeyword1 as VarChar(50);
		DECLARE @varDateRange0 as Int;
		DECLARE @varDateRange1 as Int;
		DECLARE @varItemParty0 AS VarChar(800);
		DECLARE @varItemParty1 AS VarChar(800);
		--DECLARE @varItemKeyword0 AS VarChar(250);--???Do we want a separate search for Items???
		--DECLARE @varItemKeyword1 AS VarChar(250);
	
--Keyword Search Query for ItemPartyValue(Item Table)
			SELECT a.ItemPartyValue, b.ProvenanceName, c.SeriesTitle, f.ItemTitle 
				FROM dbo.ItemParty AS a, dbo.Provenance AS b, dbo.Series AS c, dbo.Item AS f
				WHERE c.ProvenanceId=@varProvenanceID
					AND b.ProvenanceId = @varProvenanceID
					AND f.ItemId = a.ItemId
					AND ItemPartyValue LIKE '%'+@varItemParty0+'%'

--Keyword Search Query for GeoLocationValue	(Provenance, Series, and Item Tables)			
			SELECT d.GeoLocationValue, b.ProvenanceName, c.SeriesTitle, f.ItemTitle
				FROM dbo.Provenance AS b, dbo.GeoLocation AS d, dbo.Series AS c, dbo.SeriesGeoLocation_LINK AS e, dbo.Item AS f
				WHERE c.SeriesId = @varSeriesID
					AND e.SeriesId = @varSeriesID
					AND f.SeriesId = @varSeriesID
					AND b.ProvenanceId = @varProvenanceID
					AND d.GeoLocationId= e.GeoLocationId
					AND GeoLocationValue LIKE '%'+@varGeographicLocation0+'%'

--Keyword Search Query for Subject (Provenance & Series, & Item Tables)
			SELECT g.SubjectName, f.ItemTitle, b.ProvenanceName, c.SeriesTitle
				FROM dbo.Subject AS g, dbo.Provenance AS b, dbo.Series AS c, dbo.Item as f, dbo.SeriesSubject_LINK as h, dbo.ItemSubject_LINK as i
				WHERE b.ProvenanceId = @varProvenanceID
					AND c.SeriesId = @varSeriesID
					AND f.SeriesId = @varSeriesID
					AND g.SubjectId = h.SubjectId
					AND g.SubjectId = i.SubjectId
					AND g.SubjectName LIKE '%'+@varSeriesSubject0+'%'

--Keyword Search Query for Keyword (Provenance, Series, & Item Tables)				
			SELECT j.SeriesKeyword, k.Keyword, f.ItemTitle, b.ProvenanceName, c.SeriesTitle
				FROM dbo.SeriesKeyword AS j, dbo.Provenance AS b, dbo.Series AS c, dbo.Item as f, dbo.ItemKeyword as k
				WHERE b.ProvenanceId = @varProvenanceID
					AND c.SeriesId = @varSeriesID
					AND f.SeriesId = @varSeriesID
					AND j.SeriesKeyword LIKE '%'+@varSeriesKeyword0+'%'
					OR k.Keyword LIKE '%'+@varSeriesKeyword0+'%'
					--OR k.Keyword LIKE '%'+@varItemKeyword0+'%'

--Keyword Search Query for Dates (Series Table)
			IF @varDateRange1 IS NULL
				SELECT SeriesStartYear, SeriesEndYear, SeriesTitle
				FROM dbo.Series 
				WHERE ProvenanceId = @varProvenanceID
					AND SeriesId = @varSeriesID
					AND @varDateRange0 >= SeriesStartYear
			ELSE
				SELECT SeriesStartYear, SeriesEndYear, SeriesTitle
				FROM dbo.Series 
				WHERE ProvenanceId = @varProvenanceID
					AND SeriesId = @varSeriesID
					AND @varDateRange0 >= SeriesStartYear
					AND @varDateRange1 <= SeriesEndYear

--Keyword Search Query for Dates (Item Table)										
			IF @varDateRange1 IS NULL
				SELECT l.ItemDateValue, f.ItemTitle 
				FROM dbo.Series AS b, dbo.Item AS f, dbo.ItemDate as l
				WHERE b.ProvenanceId = @varProvenanceID
					AND b.SeriesId = @varSeriesID
					AND f.SeriesId = @varSeriesID
					AND f.ItemId = l.ItemId 
					AND SYSDATETIME() >= f.ItemOpenDate --created to make sure the item has no date restrictions
					AND l.ItemDateValue LIKE '%'+CAST(@varDateRange0 AS CHAR(4))+'%'
			ELSE
				SELECT l.ItemDateValue, f.ItemTitle 
				FROM dbo.Series AS b, dbo.Item AS f, dbo.ItemDate as l
				WHERE b.ProvenanceId = @varProvenanceID
					AND b.SeriesId = @varSeriesID
					AND f.SeriesId = @varSeriesID
					AND f.ItemId = l.ItemId 
					AND SYSDATETIME() >= f.ItemOpenDate --created to make sure the item has no date restrictions
					AND l.ItemDateValue >= '%'+CAST(@varDateRange0 AS CHAR(4))+'%'
					AND l.ItemDateValue <= '%'+CAST(@varDateRange1 AS CHAR(4))+'%'


	
